﻿
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports managedServer

Public Module filehost

    Public Const applicationName As String = "filehost"
    Public Const defaultConfigfile As String = "filehost.ini"

    Public Sub main(ByVal args() As String)
        ProHttpS.defaultConfigfile = defaultConfigfile
        With ProHttpS.managedRequestProcessors()
            .push_back(New storoomDelegate())
            .push_back(New put())
            .push_back(New [get]())
            .push_back(New view())
            .push_back(New homepage())
        End With
        ProHttpS.startServer(args, configNaming.filehost.name, _
                             New httpContext.parameterDefinition(stringsPackage:=sp))
    End Sub

    Public Const keyprefix As String = "filehost_"

    Public Function generateStoroomKey(ByVal filename As String) As String
        Return keyprefix + filename
    End Function

    Public Function generateTempFilename(ByVal originalFilename As String) As String
        Return originalFilename + strleft(createGUID(), 8)
    End Function

    Public Sub addviewform(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.action = strmid(view._pathidentity, strlen(network.pathSeperator))
        root.addchild(form)

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.INPUT_FILE_NAME)
        label.for = ids.viewform_file
        form.addchild(label)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.file
        input.class = "ft"
        input.id = ids.viewform_file
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.value = context.string(stringId.VIEW_FILE)
        input.class = "s"
        form.addchild(input)
    End Sub

    Public Sub addgetform(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.action = strmid([get]._pathidentity, strlen(network.pathSeperator))
        root.addchild(form)

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.INPUT_FILE_NAME)
        label.for = ids.getform_file
        form.addchild(label)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.file
        input.class = "ft"
        input.id = ids.getform_file
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.value = context.string(stringId.GET_FILE)
        input.class = "s"
        form.addchild(input)
    End Sub

    Public Sub addputform(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.action = strmid(put._pathidentity, strlen(network.pathSeperator))
        form.method = xhtmlControl_1_1.method_enum.post
        form.enctype = network.http.contentType.multipart_form_data
        root.addchild(form)

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.INPUT_FILE_NAME)
        label.for = ids.putform_file
        form.addchild(label)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.file
        input.name = query.file
        input.class = "ff"
        input.id = ids.putform_file
        form.addchild(input)

        label = context.createControl(New label())
        label.text = context.string(stringId.RENAME_FILE)
        label.for = ids.putform_rename
        form.addchild(label)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.rename
        input.class = "ft"
        input.id = ids.putform_rename
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.value = context.string(stringId.PUT_FILE)
        input.class = "s"
        form.addchild(input)
    End Sub

End Module

Public Class ids
    Public Const viewform_file As String = "vf"
    Public Const getform_file As String = "gf"
    Public Const putform_file As String = "pf"
    Public Const putform_rename As String = "pr"
End Class

Public Class query
    Public Const file As String = "f"
    Public Const rename As String = "r"
End Class
